﻿using AFanDa.BLL.BaseService;
using AFanDa.Dto.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AFanDa.BLL.BLLService
{
    public class GoodsService
    {
        BaseGoodsLabel _baseGoodsLabel = null;
        BaseGoods _baseGoods = null;
        BaseGoodsType _baseGoodsType = null;
        BaseFlavorTypeService _baseFlavorTypeService = null;
        BaseflavorService _baseFlavorService = null;
        public GoodsService()
        {
            _baseGoods = new BaseGoods();
            _baseGoodsLabel = new BaseGoodsLabel();
            _baseGoodsType = new BaseGoodsType();
            _baseFlavorTypeService = new BaseFlavorTypeService();
            _baseFlavorService = new BaseflavorService();
        }


        /// <summary>
        /// 获取所有商品
        /// </summary>
        /// <returns></returns>
        public ApiResult getGoods(Dto.Request.GoodsRequestDto dto)
        {
            Expression<Func<Dto.EF.goods, bool>> where = d => d.Gname.Contains(dto.key);

            if (dto.Glid != "")
            {
                where = d => d.Gname.Contains(dto.key) && d.GGlid == dto.Glid;
            }
            if (dto.status != -1)
            {
                where = d => d.Gname.Contains(dto.key) && d.Gstatus == dto.status;
            }


            if (dto.Glid != "" && dto.status != -1)
            {
                where = d => d.Gname.Contains(dto.key) && d.GGlid == dto.Glid && d.Gstatus == dto.status;

            }

            var iq = _baseGoods.QueryAll(where);

            var data = iq.Select(d => new Dto.Reponse.GoodsResponseDto()
            {
                id = d.Gid,
                name = d.Gname,
                detail = d.Gdetail,
                price = d.Gprice,
                img = d.GImg,
                status = d.Gstatus,
                num = d.Gnum,
                FlavorsTypeName = d.flavorType.FTName

            });

            return new ApiResult()
            {
                code = ApiCode.ok,
                message = "获取商品",
                data = data
            };

        }







        /// <summary>
        /// 获取商品类型
        /// </summary>
        /// <returns></returns>
        public ApiResult GetGoodsType(string keys)
        {
            var iq = _baseGoodsType.QueryAll(d => d.Gtname.Contains(keys));

            var data = iq.Select(d => new Dto.Reponse.GoodsTypeResponseDto()
            {
                id = d.Gtid,
                name = d.Gtname,
                img = d.GtImg,
                labelCount = d.goodsLabel.Count
            }).ToList();

            return new ApiResult()
            {
                code = ApiCode.ok,
                message = "获取商品类型",
                data = data
            };
        }

        /// <summary>
        /// 添加商品类型
        /// </summary>
        /// <param name="Dto"></param>
        /// <returns></returns>
        public ApiResult AddGoodType(Dto.Request.GoodsTypeRequestDto Dto)
        {



            _baseGoodsType.Add(new AFanDa.Dto.EF.goodsType
            {
                Gtid = Dto.id,
                Gspare = Dto.spare,
                Gtname = Dto.name,
                GtImg = Dto.img
            });



            var result = _baseGoodsType.SaveChange();




            return new ApiResult()
            {
                code = result > 0 ? ApiCode.ok : ApiCode.fail,
                message = result > 0 ? "添加成功" : "添加失败"

            };

        }
        /// <summary>
        /// 添加 商品标签
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult AddGoodsLabel(Dto.Request.GoodsLabelRequestDto dto)
        {
            _baseGoodsLabel.Add(new Dto.EF.goodsLabel()
            {
                Glid = Common.Config.GUID(),
                GlGTid = dto.Gtid,
                Glname = dto.name
            });

            var result = _baseGoodsLabel.SaveChange();
            if (result > 0)
            {
                return new ApiResult()
                {
                    code = ApiCode.ok,
                    message = "添加成功"
                };
            }

            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "添加失败"

            };


        }

        /// <summary>
        /// 根据 商品类型 id 获取商品标签
        /// </summary>
        /// <returns></returns>
        public ApiResult GetGoodslabelByGTid(Dto.Request.GoodsTypeRequestDto dto)
        {


            Expression<Func<Dto.EF.goodsLabel, bool>> where = null;


            if (dto.id != "")
            {
                where = d => d.GlGTid == dto.id && d.Glname.Contains(dto.key);
            }


            var iq = _baseGoodsLabel.QueryAll(where);

            var data = iq.Select(d => new Dto.Reponse.GoodsLabelResponseDto()
            {
                id = d.Glid,
                name = d.Glname,
                goodsCount = d.goods.Count,
                goodsTypeName = d.goodsType.Gtname
            }).ToList();

            return new ApiResult()
            {
                code = 0,
                message = "根据商品类型 id 查询商品标签",
                data = data
            };
        }

        /// <summary>
        /// 添加口味类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult AddFlavorType(Dto.Request.FlavorTypeRequestDto dto)
        {
            _baseFlavorTypeService.Add(new Dto.EF.flavorType()
            {
                FTid = Common.Config.GUID(),
                FTcreateTime = DateTime.Now,
                FTName = dto.name
            });

            var result = _baseFlavorTypeService.SaveChange();

            if (result > 0)
            {
                return new ApiResult()
                {
                    code = ApiCode.ok,
                    message = "添加口味成功",
                };
            }
            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "添加口味成功",
            };


        }



        /// <summary>
        /// 获取口味类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult GetFlavorType(Dto.Request.FlavorTypeRequestDto dto)
        {
            var iq = _baseFlavorTypeService.QueryAll(d => d.FTName.Contains(dto.name));
            var data = iq.Select(d => new Dto.Reponse.FlavorTypeResponseDto()
            {
                id = d.FTid,
                name = d.FTName,
                FlaoveCount = d.flavor.Count
            }).ToList();

            return new ApiResult()
            {
                code = 0,
                message = "获取口味类型",
                data = data
            };
        }


        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public ApiResult AddGoods(Dto.Request.GoodsRequestDto dto)
        {
            _baseGoods.Add(new Dto.EF.goods()
            {
                Gid = dto.id,
                GGlid = dto.Glid,
                GFTid = dto.Ftid,
                GImg = dto.img,
                Gname = dto.name,
                Gdetail = dto.details,
                Gnum = dto.num,
                Gprice = dto.price,
                Gorder = 0,
                Gstatus = 1,
                SameDayNum = 0
            });

            var result = _baseGoods.SaveChange();

            if (result > 0)
            {
                return new ApiResult()
                {
                    code = ApiCode.ok,
                    message = "添加商品成功"
                };
            }
            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "添加失败"
            };
        }


        /// <summary>
        /// 获取所有商品 
        /// </summary>
        /// <returns></returns>
        public ApiResult GetAllGoods()
        {
            var iq = _baseGoodsType.QueryAll(d => d.goodsLabel.Count > 0);

            var data = iq.Select(d => new Dto.Reponse.GoodsTypeResponseDto()
            {
                id = d.Gtid,
                name = d.Gtname,
                img = d.GtImg,
                GoodsLabel = d.goodsLabel.Select(L => new Dto.Reponse.GoodsLabelResponseDto()
                {
                    id = L.Glid,
                    name = L.Glname,
                    Goods = L.goods.Where(l => l.Gstatus == 0).Select(g => new Dto.Reponse.GoodsResponseDto()
                    {
                        id = g.Gid,
                        name = g.Gname,
                        detail = g.Gdetail,
                        price = g.Gprice,
                        SameDayNum = g.SameDayNum,
                        img = g.GImg,
                        status = g.Gstatus,
                        isFlovar = g.GFTid == null ? false : true,
                        Flavors = g.flavorType.flavor.Select(f => new Dto.Reponse.Flavor()
                        {
                            id = f.Fid,
                            name = f.Fname,

                        }).ToList()

                    }).ToList()
                }).ToList()
            }).ToList();




            return new ApiResult()
            {
                code = ApiCode.ok,
                message = "获取所有商品",
                data = data
            };

        }
        /// <summary>
        /// 获取口味 根据口味类型
        /// </summary>
        /// <returns></returns>
        public ApiResult GetFlavor(Dto.Request.flavorRequestDto dto)
        {
            var iq = _baseFlavorService.QueryAll(d => d.Fname.Contains(dto.key) && d.FTid == dto.Ftid);

            var data = iq.Select(d => new Dto.Reponse.Flavor()
            {
                name = d.Fname,
                id = d.Fid,
                FtName = d.flavorType.FTName
            }).ToList();

            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "获取口味",
                data = data
            };
        }

        /// <summary>
        /// 添加口味
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult AddFlavor(Dto.Request.flavorRequestDto dto)
        {
            _baseFlavorService.Add(new Dto.EF.flavor()
            {
                Fid = Common.Config.GUID(),
                Fname = dto.name,
                Fcreatetime = DateTime.Now,
                FTid = dto.Ftid
            });

            var result = _baseFlavorService.SaveChange();


            return new ApiResult()
            {
                code = result > 0 ? ApiCode.ok : ApiCode.fail,
                message = result > 0 ? "添加成功" : "添加失败"
            };


        }

        /// <summary>
        /// 修改口味
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult UpdateFlavor(Dto.Request.flavorRequestDto dto)
        {

            var iq = _baseFlavorService.QueryAll(d => d.Fid == dto.id);

            if (iq.Any())
            {

                var flavor = iq.Single();
                flavor.Fid = dto.id;
                flavor.FTid = dto.Ftid;
                flavor.Fname = dto.name;

                _baseFlavorService.Update(flavor);

                var result = _baseFlavorService.SaveChange();


                return new ApiResult()
                {
                    code = result > 0 ? ApiCode.ok : ApiCode.fail,
                    message = result > 0 ? "修改成功" : "修改失败"
                };
            }
            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "口味id错误"
            };



        }

        /// <summary>
        /// 修改口味类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult UpdateFlavorType(Dto.Request.FlavorTypeRequestDto dto)
        {

            var iq = _baseFlavorTypeService.QueryAll(d => d.FTid == dto.id);
            if (iq.Any())
            {
                var FlavorType = iq.Single();

                FlavorType.FTid = dto.id;
                FlavorType.FTName = dto.name;
                _baseFlavorTypeService.Update(FlavorType);

                var result = _baseFlavorTypeService.SaveChange();
                if (result > 0)
                {
                    return new ApiResult()
                    {
                        code = ApiCode.ok,
                        message = "修改成功"
                    };
                }
                return new ApiResult()
                {
                    code = ApiCode.fail,
                    message = "修改失败"
                };
            }

            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "口味类型id错误"
            };




        }


        /// <summary>
        /// 修改商品标签
        /// </summary>
        /// <returns></returns>
        public ApiResult UpdateGoodLabel(Dto.Request.GoodsLabelRequestDto dto)
        {
            var iq = _baseGoodsLabel.QueryAll(d => d.Glid == dto.id);
            if (iq.Any())
            {
                var GoodsLabel = iq.Single();
                GoodsLabel.Glid = dto.id;
                GoodsLabel.GlGTid = dto.Gtid;
                GoodsLabel.Glname = dto.name;


                _baseGoodsLabel.Update(GoodsLabel);
                var result = _baseGoodsLabel.SaveChange();


                return new ApiResult()
                {
                    code = result > 0 ? ApiCode.ok : ApiCode.fail,
                    message = result > 0 ? "修改成功" : "修改失败"
                };

            }
            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "商品标签id错误"
            };


        }

        /// <summary>
        /// 修改商品类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult UpdateGoodsType(Dto.Request.GoodsTypeRequestDto dto)
        {
            var iq = _baseGoodsType.QueryAll(d => d.Gtid == dto.id);

            if (iq.Any())
            {
                var GoodsType = iq.Single();

                GoodsType.Gtid = dto.id;
                GoodsType.Gtname = dto.name;
                GoodsType.GtImg = dto.img;

                _baseGoodsType.Update(GoodsType);

                var result = _baseGoodsType.SaveChange();


                return new ApiResult()
                {
                    code = result > 0 ? ApiCode.ok : ApiCode.fail,
                    message = result > 0 ? "修改成功" : "修改失败"
                };


            }

            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "商品标签id错误"
            };
        }

        /// <summary>
        /// 修改商品状态
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult UpdateGoodStatus(Dto.Request.GoodsRequestDto dto)
        {
            var iq = _baseGoods.QueryAll(d => d.Gid == dto.id);

            if (iq.Any())
            {
                var good = iq.Single();

                good.Gid = dto.id;
                good.Gstatus = dto.status;

                _baseGoods.Update(good);


                var result = _baseGoods.SaveChange();

                if (result > 0)
                {
                    return new ApiResult()
                    {
                        code = ApiCode.ok,
                        message = "操作成功"
                    };

                }


            }

            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "商品id不存在"
            };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult GetGoodsById(Dto.Request.GoodsRequestDto dto)
        {

            var iq = _baseGoods.QueryAll(d => d.Gid == dto.id);

            if (iq.Any())
            {
                var good = iq.Single();

                var data = new Dto.Reponse.GoodsResponseDto()
                {
                    id = good.Gid,
                    img = good.GImg,
                    detail = good.Gdetail,
                    FlavorsTypeName = good.flavorType==null?null:good.flavorType.FTName,
                    num = good.Gnum,
                    price = good.Gprice,
                    SameDayNum = good.SameDayNum,
                    status = good.Gstatus,
                    name = good.Gname,
                    GoodLabelName = good.goodsLabel.Glname,
                    GoodTypeName = good.goodsLabel.goodsType.Gtname

                };


                return new ApiResult()
                {
                    code = ApiCode.ok,
                    data = data
                };


            }

            return new ApiResult()
            {

                code = ApiCode.fail,
                message = "参数错误"
            };

        }
    }
}
